package com.secondGroup.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.secondGroup.entity.BankCard;
import com.secondGroup.entity.User;
import com.secondGroup.entity.VirtualBankCard;
import com.secondGroup.mapper.VirtualBankCardMapper;
import com.secondGroup.service.BankCardService;
import com.secondGroup.service.UserService;
import com.secondGroup.service.VirtualBankCardService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.Wrapper;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ws
 * @since 2022-05-05
 */
@Controller

public class VirtualBankCardController {
    @Autowired
    VirtualBankCardService virtualBankCardService;
    @Autowired
    BankCardService bankCardService;
    @Autowired
    UserService userService;

    /**
     *绑卡验证
     * @Author ybk
     */
    @RequestMapping("/verification")
    public String verification(String idNumber, String cid, HttpServletRequest request, Model model){

        VirtualBankCard virtualBankCard  = virtualBankCardService.selectverification(cid,idNumber);
        //方便测试，将account为11的用户信息放入session中；
//        User byAccount = userService.getOne(new QueryWrapper<User>()
//        .lambda()
//        .eq(User::getAccount,"11"));
//        session.setAttribute("userInfo",byAccount);

        User userInfo = (User) request.getSession().getAttribute("userInfo");
        String account=userInfo.getAccount();
        if(virtualBankCard == null){
            model.addAttribute("message","此银行卡不存在,请核对银行卡信息");
            return "fail";
        }else {
            BankCard bankCard = bankCardService.find(cid);
            if (bankCard != null){
                model.addAttribute("message","此卡绑定过");
                return "fail";
            }else {
                bankCardService.insert(account,cid);
                model.addAttribute("message","绑定成功");
                return "bindSuccess";
            }

        }

    }
    /**
     * 返回显示
     */
    @RequestMapping("/tieCard")
    public String listVirtualBankCard(Model model, HttpServletRequest request, HttpSession session){
//        User byAccount = userService.getOne(new QueryWrapper<User>()
//                .lambda()
//                .eq(User::getAccount,"11"));
//        session.setAttribute("userInfo",byAccount);
        User userInfo = (User) request.getSession().getAttribute("userInfo");
        String account=userInfo.getAccount();
        List<VirtualBankCard> virtualBankCards = virtualBankCardService.findAll(account);
        model.addAttribute("virtualBankCards",virtualBankCards);
        return "tieCard";
    }
}
